/**
 * Created by c on 2017-11-15.
 */

$(function(){
    getDrugType();
})

//加载查询框多选框
function getDrugType(){
    //获取药品分类信息
    $.post(ctx+"/drug/getDrugType",null,function(data){
        $(data).each(function(){
            var $opt = $("<option/>");
            $opt.val(this.typeNum);
            $opt.html(this.typeName);
            $opt.appendTo("#xia");
        });
    },"json");
}

function initAddDrug(){
    $("div#addDrug").children().remove();
    $("div#updateDrug").children().remove();
    $("div.myForm form").clone().appendTo("#addDrug");
    $("form p span").children().remove();
    //加载模态框
    $("#addDrug").dialog({
        title:"药品信息登记",
        btnValue:"添加",
        addBind:function(){
            if(!checkInput()){
                return;
            }
            var arr = $("#addDrug form").serialize();
            console.log(arr)
            $.post(ctx+"/drug/addDrug",arr,function(msg){
                toastr.success("操作提示",msg);
                $("#addDrug").dialog("close");
                $("#drugTable").bootstrapTable("refresh");
            });
        }
    });
}

function initUpdateDrug(){
    $("div#addDrug").children().remove();
    $("div#updateDrug").children().remove();
    $("div.myForm form").clone().appendTo("#updateDrug");
    $("form p span").children().remove();
    //加载模态框
    $("#updateDrug").dialog({
        title:"药品信息登记",
        btnValue:"修改",
        addBind:function(){
            if(!checkInput()){
                return;
            }
            var upArr = $("#updateDrug form").serialize();
            $.post(ctx+"/drug/updateDrug",upArr,function(msg){
                toastr.success("操作提示",msg);
                $("#updateDrug").dialog("close");
                $("#drugTable").bootstrapTable("refresh");
            },"text");
        }
    });
}

function checkName(){
    //名称
    var drugName = $(".modal-body input[name=drugName]").val();
    $("span.checkName").html(" ").css("color","red");
    if(drugName == ""){
        $("span.checkName").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<span>请填写药品名称!</span>');
        return false;
    } else {
        $("span.checkName").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}

function checkStandard(){
    //规格
    var drugStandard = $("input[name='drugStandard']").val();
    $("span.checkStandard").html(" ").css("color","red");
    if(drugStandard == ""){
        $("span.checkStandard").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请填写药品规格!</i>');
        return false;
    } else {
        $("span.checkStandard").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}

function checkUnit(){
    //d单位
    var drugUnit = $("input[name='drugUnit']").val();
    $("span.checkUnit").html(" ").css("color","red");
    if(drugUnit == ""){
        $("span.checkUnit").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请填写药品单位!</i>');
        return false;
    } else {
        $("span.checkUnit").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}

function checkPreparation(){
    //剂型
    var drugPreparation = $("input[name='drugPreparation']").val();
    $("span.checkPreparation").html(" ").css("color","red");
    if(drugPreparation == ""){
        $("span.checkPreparation").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请填写药品剂型!</i>');
        return false;
    } else {
        $("span.checkPreparation").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}

function checkWarranty(){
    //质保期
    var drugWarranty = $("input[name='drugWarranty']").val();
    $("span.checkWarranty").html(" ").css("color","red");
    if(drugWarranty == ""){
        $("span.checkWarranty").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请填写药品质保期!</i>');
        return false;
    } else {
        $("span.checkWarranty").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}

function checkstoFloor(){
    //库存下限
    var drugFloor = $("input[name='drugFloor']").val();
    $("span.checkstoFloor").html(" ").css("color","red");
    if(drugFloor == ""){
        $("span.checkstoFloor").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请填写库存下限!</i>');
        return false;
    } else {
        $("span.checkstoFloor").html('<i class="layui-icon" style="color:green;"></i>');
    }
    return true;
}

function checktypeNum(value){
    $("span.checktypeNum").html(" ").css("color","red");
    if(value == "-1"){
        $("span.checktypeNum").html('<i class="layui-icon" style="color:red;">ဇ</i>&nbsp;<i>请选择药品类型!</i>');
        return false;
    }
        $("span.checktypeNum").html('<i class="layui-icon" style="color:green;"></i>');
        return true;
}


function clearN(value){
    $(value).parent().next().children().children().remove();
}

//验证用户填写的信息
function checkInput(){
    if(!checkName()||!checkStandard()||!checkUnit()||!checkPreparation()||!checkWarranty()||!checktypeNum()||!checkWarranty()){
        return false;
    }
    return true;
}

//加载表格操作
function caozuo (){
    var cao = '<button class="layui-btn layui-btn-normal layui-btn-mini updateDrug">编辑</button>' +
        '<button type="button"  class="layui-btn layui-btn-danger layui-btn-mini del">删除</button>';
    return cao;
}

window.optEvents= {
    "click .updateDrug": function (e,value,row) {
        updateDrug(row);
    },
    "click .del":function(e,value,row){
        delOne(row);
    }
}

//模糊查询
function search(){
    var param={};
    param.drugNum=$("input[name='drugNum']").val();
    param.drugName=$("input[name='drugName']").val();
    param.typeNum=$("select[name='typeNum']").val();
    $("#drugTable").bootstrapTable("refresh",{query:param});
}

//删除单个药品信息
function delOne(row){
    swal({
        title:"确认提示",
        text:"确认删除选中的药品信息?",
        type:"warning",
        showCancelButton:true,
        closeOnConfirm:false,
        confirmButtonText:"删除",
        confirmButtonColor:"#DD6B55",
        cancelButtonText: "取消"
    }).then(function(){
        var param = "drugNum="+row.drugNum;
        $.post(ctx+"/drug/batchDel",param,function(msg){

            if(msg.stockError!=null && msg.stockError!=''){
                //库存中存有信息不能删除
                swal({
                    title:"警告",
                    text:msg.stockError,
                    type:"error"
                })
            }
            if(msg.infoError!=null && msg.infoError!=''){
                //其他信息表中有引用该药品信息，提示是否一并删除
                swal({
                    title:"警告",
                    text:msg.infoError,
                    type:"warning",
                    showCancelButton:true,
                    closeOnConfirm:false,
                    confirmButtonText:"一并删除",
                    confirmButtonColor:"#DD6B55",
                    cancelButtonText: "取消"
                }).then(function(){
                    $.post(ctx+"/drug/batchDelAll",param,function(msgInfo){
                        if(msgInfo.delSuccess!=null && msgInfo.delSuccess!=""){
                            $("#drugTable").bootstrapTable("refresh");
                            toastr.success("操作提示",msgInfo.delSuccess);
                        }
                    },"json");
                });
            }

            if(msg.delSuccess!=null && msg.delSuccess!=''){
                $("#drugTable").bootstrapTable("refresh");
                toastr.success("操作提示",msg.delSuccess);
            }
        },"json");
    })
}

//批量删除药品信息
function batchDel(){
    var arr = $("#drugTable").bootstrapTable("getSelections");
    if(arr.length==0){
        swal({
            title:"提示",
            text:"请至少选择一项待删除!",
            type:"warning"
        })
        return;
    }
    swal({
        title:"确认提示",
        text:"确认删除选中的药品信息?",
        type:"warning",
        showCancelButton:true,
        closeOnConfirm:false,
        confirmButtonText:"删除",
        confirmButtonColor:"#DD6B55",
        cancelButtonText: "取消"
    }).then(function(){
        var param = $(arr).map(function(){
         return "drugNum="+this.drugNum;
         }).get().join("&");
         $.post(ctx+"/drug/batchDel",param,function(msg){

             if(msg.stockError!=null && msg.stockError!=''){
                 //库存中存有信息不能删除
                 swal({
                     title:"警告",
                     text:msg.stockError,
                     type:"error"
                 })
             }
             if(msg.infoError!=null && msg.infoError!=''){
                 //其他信息表中有引用该药品信息，提示是否一并删除
                 swal({
                     title:"警告",
                     text:msg.infoError,
                     type:"warning",
                     showCancelButton:true,
                     closeOnConfirm:false,
                     confirmButtonText:"一并删除",
                     confirmButtonColor:"#DD6B55",
                     cancelButtonText: "取消"
                 }).then(function(){
                     $.post(ctx+"/drug/batchDelAll",param,function(msgInfo){
                        if(msgInfo.delSuccess!=null && msgInfo.delSuccess!=""){
                            $("#drugTable").bootstrapTable("refresh");
                            toastr.success("操作提示",msgInfo.delSuccess);
                        }
                     },"json");
                 });
             }

             if(msg.delSuccess!=null && msg.delSuccess!=''){
                 $("#drugTable").bootstrapTable("refresh");
                 toastr.success("操作提示",msg.delSuccess);
             }
         },"json");
    })
}

//添加药品信息
function addDrug(){
    initAddDrug();
    $("#addDrug").dialog("open");
    //获取药品分类信息
    $.post(ctx+"/drug/getDrugType",null,function(data){
        $(data).each(function(){
            var $opt = $("<option/>");
            $opt.val(this.typeNum);
            $opt.html(this.typeName);
            $opt.appendTo("#sel");
        });
    },"json");
}

//修改药品信息
function updateDrug(row){
    initUpdateDrug();

    $("#updateDrug").dialog("open");
    //获取药品分类信息
    $.post(ctx+"/drug/getDrugType",null,function(data){
        $(data).each(function(){
            var $opt = $("<option/>");
            $opt.val(this.typeNum);
            $opt.html(this.typeName);
            $opt.appendTo("#sel");
        });
        //加载药品信息
        $("#updateDrug form").setForm(row);
    },"json");
}





